package com.choco.producer;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

/**
 * @author choco
 * @date 2021-11-20 13:06
 */
@Slf4j
public class Producer {
    public static void main(String[] args) {
        Properties props= new Properties();
        //kafka 集群，broker-1ist
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        props.put("acks", "all");
        //重试次数
         props.put("retries",1);
        //批次大小 16k
        props.put("batch.size", 16384);
        //等待时间 1ms
        props.put("linger.ms", 1);
        //recordAccumulator 缓冲区大小 32M
        props.put("buffer.memory", 33554432);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put ("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<>(props);

        for (int i = 0; i < 10; i++) {
            kafkaProducer.send(new ProducerRecord<>("first", "choco--" + i));
        }

        kafkaProducer.close();
    }
}
